<template>
  <div class="tab_class">
    <div class="tal_class_searchBox">
      <van-search shape="round"  placeholder="点击前往搜索" />
      <div class="tal_class_searchMask" @click="$router.push({ name: 'search' })"></div>
    </div>

    <div class="class_tree clearfix">
      <ul class="class_tree_nav">
        <li
          v-for="(item, index) in categoryList"
          :key="index"
          :class="{active_nav: currentCategory.id == item.id}"
          @click="changeCatalog(item.id)"
        >{{item.name}}</li>
      </ul>
      <div class="class_tree_content">
        <item-list ref="vantGoods" :catalog="currentCategory.id"></item-list>
        <!-- <div class="class_tree_all">
          <img style="width:100%" v-lazy="currentCategory.picUrl" />
        </div>-->
        <!-- <div class="box">
          <span>{{currentCategory.desc}}</span>
        </div>-->
        <!-- <div class="class_tree_items_wrap clearfix">
          <div @click="toItemList(item.id)" :key="i" v-for="(item, i) in currentSubCategoryList">
            <div class="class_tree_item_img">
              <img :src="item.picUrl" :alt="item.name" />
            </div>
            <div class="class_tree_item_name">{{item.name}}</div>
          </div>
        </div>-->
      </div>
    </div>
  </div>
</template>

<script>
import { catalogList, catalogCurrent } from '@/api/api';
import ItemList from './goods';
import { Search } from 'vant';

export default {
  components: {
    [Search.name]: Search,
    ItemList
  },
  data() {
    return {
      categoryList: [
        // { id: 'a', name: '商品分类' },
        // { id: 0, name: '商品分类0' },
        // { id: 1, name: '商品分类1' },
        // { id: 2, name: '商品分类2' },
        // { id: 3, name: '商品分类3' }
      ],
      currentCategory: {
        id: '',
        desc: '分类的描述分类的描述分类的描述分类的描述分类的描述'
      },
      currentSubCategoryList: []
    };
  },

  activated() {
    this.initData();
  },

  methods: {
    initData() {
      const categoryId=this.$route.params.id;
      catalogList().then(res => {
        let data = res.data.result;
        this.categoryList = data;
        if(this.categoryList.length>0){
          if(categoryId!=-1){
            this.currentCategory.id=categoryId;
          }else{
            this.currentCategory.id=this.categoryList[0].id;
          }
          this.$refs.vantGoods.init(this.currentCategory.id);
        }
        // this.currentCategory = res.data.data.currentCategory;
        // this.currentSubCategoryList = data.currentSubCategory;
      });
    },
    changeCatalog(id) {
      // 获取分类详情，成功后切换标签的激活状态
      this.currentCategory.id = id;
      if(id!=""){
        this.$refs.vantGoods.init(id); // 初始化分类下的商品
      }

    }
    // toItemList(id) {
    //   this.$router.push({
    //     name: 'category',
    //     query: { keyword: '', itemClass: id }
    //   });
    // }
  }
};
</script>


<style lang="scss" scoped>
@import '../../assets/scss/mixin';

.tab_class {
  overflow: hidden;
  background-color: #fff;
}

.height-fix {
  padding-bottom: 42px;
}

.tal_class_searchBox {
  position: relative;
}

.tal_class_searchMask {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 9;
}

.class_tree {
  display: flex;
  position: relative;
  background-color: #fff;
  overflow: hidden;
  height: 100%;
  box-sizing: border-box;
}
.class_tree_nav {
  width: 100px;
  height: 100%;
  background-color: #fff;
  overflow: scroll;
  border-right: 1px solid $bg-color;
  > li {
    @include one-border;
    height: 40px;
    width: 80px;
    line-height: 40px;
    text-align: center;
    overflow-x: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  > li.active_nav {
    background-color: #fff;
    border-left: 2px solid $red;
    color: $red;
  }
}
.box {
  text-align: center;
  font-family: PingFangSC-Light, helvetica, 'Heiti SC';
  font-size: 13px;
  border-bottom: 1px solid $bg-color;
  padding-bottom: 12px;
}
.box span {
  line-height: 20px;
}
.class_tree_content {
  flex: 1;
  height: 100%;
  overflow-y: scroll;
  .class_tree_all {
    text-align: right;
    color: $font-color-gray;
    font-size: $font-size-small;
  }
  .van-icon-arrow {
    font-size: $font-size-small;
  }
  .class_tree_items_wrap {
    padding: 10px 20px;
    margin-right: -3%;
    margin-top: 70px;
    text-align: center;
    > div {
      float: left;
      padding-right: 3%;
      box-sizing: border-box;
      width: 33.333%;
      margin-bottom: 20px;
    }
    img {
      max-width: 100%;
    }

    .class_tree_item_img {
      display: inline-block;
      max-width: 100%;
      width: 70px;
      height: 70px;
    }
    .class_tree_item_name {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
}
</style>
